package org.example.config;

import org.example.interceptors.UserInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //需要放行
        ArrayList<String> acl = new ArrayList();
        //放行下列swagger相关路径
        acl.add("/webjars/**");
        acl.add("/doc.html");
        acl.add("/swagger-resources");
        acl.add("/v2/api-docs");
        acl.add("/registerUser");
        acl.add("/login");

        //配置拦截器
        registry.addInterceptor(new UserInterceptor())
                //拦截
                .addPathPatterns("/**")
                //放行
                .excludePathPatterns(acl);
    }

    /**
     * 设置静态资源映射
     * @param registry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}